<template>
    <view>
        <!-- 导航 -->
		<view class="nav bg-white">
		    <block v-for="(item, index) in nav_status_list" :key="index">
		        <view v-if="nav_status_index == index" class="item fl tc cr-main" :data-index="index" @tap="nav_event"><text>{{item.name}}</text></view>
		        <view v-else class="item fl tc" :data-index="index" @tap="nav_event">{{item.name}}</view>
		    </block>
		</view>

        <!-- 订单列表 -->
		<scroll-view :scroll-y="true" class="scroll-box" @scrolltolower="scroll_lower" lower-threshold="30">
		    <view v-if="data_list.length > 0" class="padding-horizontal-main padding-top-main">
		        <view v-for="(item, index) in data_list" :key="index" class="list-item padding-horizontal-main padding-top-main border-radius-main bg-white oh spacing-mb">
		            <view class="item-base oh br-b padding-bottom-main">
		                <!-- 基础信息 -->
		                <view class="fl">
		                    <image class="warehouse-group-icon va-m margin-right-sm" :src="groups_static_url+'mini/groups-team.png'" mode="aspectFit"></image>
		                    <text class="cr-base va-m">{{item.add_time}}</text>
		                </view>
		                <text class="fr cr-red">{{item.status_name}}<text v-if="(item.is_under_line_text || null) != null">（{{item.is_under_line_text}}）</text></text>
		            </view>
		            <view v-for="(detail, di) in item.items" :key="di" class="br-b-dashed oh padding-vertical-main">
		                <navigator :url="'/pages/fcc/groups/teamdetail/teamdetail?id=' + item.id" hover-class="none">
		                    <image class="goods-image fl radius" :src="detail.images" mode="aspectFill"></image>
		                    <view class="goods-base pr">
		                        <view class="multi-text">{{detail.title}}</view>
		                        <view v-if="detail.spec != null" class="margin-top-sm">
		                            <block v-for="(sv, si) in detail.spec" :key="si">
		                                <text v-if="si > 0" class="cr-grey padding-left-xs padding-right-xs">;</text>
		                                <text class="cr-grey">{{sv.value}}</text>
		                            </block>
		                        </view>                            
		                        <view class="margin-top-sm">
		                            <text class="fw-b">{{currency_symbol}}{{detail.price}}</text>
		                            <text class="margin-left-sm">x{{detail.buy_number}}</text>
		                        </view>
		                    </view>
		                </navigator>
		            </view>
		            <view class="padding-vertical-main tr cr-base text-size">
		                <text>{{item.describe}}</text>
		            </view>
		            <view v-if="(item.status >= 2 && item.order_model != 2)" class="item-operation tr br-t padding-vertical-main">
						<button class="round bg-white cr-green br-green" type="default" size="mini"@tap="url_event" :data-value="'/pages/fcc/groups/teamdetail/teamdetail?id='+item.id" :data-index="index" hover-class="none">{{lang_info.order_detail_view_group_details}}</button>
		            </view>
		        </view>
		    </view>
		    <view v-else>
		        <!-- 提示信息 -->
		        <component-no-data :propStatus="data_list_loding_status"></component-no-data>
		    </view>
		
		    <!-- 结尾 -->
		    <component-bottom-line :propStatus="data_bottom_line_status" :propMsg="lang_info.data_bottom_line_msg"></component-bottom-line>
		</scroll-view>
		
		<!-- 红包弹窗 yxpopup -->
		<component-yxpopup :propShow="is_show_hongbao_popup" propPosition="center" propClassname="hongbao" @onclose="hongbao_popup_event_close">
			<view v-if="plugins_hongbao_data.length > 0" class="hongbao_box">
				<view class="item" v-for="(item, index) in plugins_hongbao_data" :key="index">
					<view v-if="index==0" class="item-content pr" :style="hongbao_bg_style">
						<view class="hongbao_send_people">
							<image class="icon auto dis-inline-block" :src="home_site_logo_square" mode="widthFix"></image>
							<text class="dis-inline-block">{{lang_info.groups_hongbao_tips_title}}</text>
						</view>
						<view class="daojishi">
							<view class="dis-inline-block">{{lang_info.groups_hongbao_get_time_text}} </view>
							<view class="dis-inline-block">
							<component-countdown :propHour="item.time.hours" :propMinute="item.time.minutes" :propSecond="item.time.seconds"></component-countdown>
							</view>
						</view>
						<view v-if="item.buchang_time == 0" class="gotolingqu" :data-value="item.id" @click="handle_hongbao_event">{{lang_info.groups_hongbao_open_text}}</view>
					</view>
				</view>
				<view class="hongbao_close" @tap="hongbao_popup_event_close"></view>
			</view>
		</component-yxpopup>

        <!-- 结尾 -->
        <component-footer :propStatus="3"></component-footer>
    </view>
</template>

<script>
const app = getApp();
import componentPopup from '@/components/popup/popup';
import componentQuickNav from "@/components/quick-nav/quick-nav";
import componentBadge from '@/components/badge/badge';
import componentNoData from "@/components/no-data/no-data";
import componentBottomLine from "@/components/bottom-line/bottom-line";
import componentOnlineService from "@/components/online-service/online-service";
import componentFooter from "@/pages/fcc/groups/public/footer";
import componentCountdown from "@/pages/fcc/groups/public/countdown";
import componentYxpopup from "@/pages/fcc/groups/public/yxpopup";
import { groups_base_info, clear_user_cach } from '@/pages/fcc/groups/public/common.js';

var groups_static_url = app.globalData.get_static_url('groups', true);
var common_static_url = app.globalData.get_static_url('common');
export default {
    components: {
        componentQuickNav,
        componentPopup,
        componentBadge,
        componentCountdown,
        componentNoData,
        componentBottomLine,
        componentOnlineService,
		componentFooter,
		componentYxpopup
    },
    data() {
        return {
			currency_symbol: app.globalData.currency_symbol(),
			groups_static_url:groups_static_url,
			common_static_url:common_static_url,
            data_list: [],
            data_page_total: 0,
            data_page: 1,
            data_list_loding_status: 1,
            data_bottom_line_status: false,
            params: null,
            input_keyword_value: '',
            load_status: 0,
            is_show_payment_popup: false,
            payment_list: [],
            payment_id: 0,
            temp_pay_value: 0,
            temp_pay_index: 0,

            nav_status_list: [],

            nav_status_index: 0,
            data_total: '',

            detail: {
                key: '',
                images: '',
                title: '',
                spec: '',
                price: '',
                original_price: 0,
                buy_number: ''
            },

            spec: {
                key: '',
                type: '',
                value: ''
            },

            data_list_loding_msg: '',
			// 红包数据
			plugins_hongbao_data:[],
			is_show_hongbao_popup:false,
			hongbao_bg_style:'',
			home_site_logo_square: null,
			
			// 多语言
			lang_info:{},
        };
    },
    onLoad(params) {
        // 是否指定状态
        var nav_status_index = 0;

        if ((params.status || null) != null) {
            for (var i in this.nav_status_list) {
                if (this.nav_status_list[i]['value'] == params.status) {
                    nav_status_index = i;
                    break;
                }
            }
        }

        this.setData({
            params: params,
            nav_status_index: nav_status_index
        });
    },
    onShow() {
		this.init_config();
        this.init();
		
		uni.setNavigationBarTitle({
		    title: ''
		});
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            data_page: 1
        });
        this.get_data_list(1);
		this.get_other_data();
    },
    methods: {
		// 初始化配置
		init_config(status) {
		    if ((status || false) == true) {
		        this.setData({
					currency_symbol: app.globalData.get_config('currency_symbol'),
					common_app_is_online_service: app.globalData.get_config('config.common_app_is_online_service'),
		            home_site_logo_square: app.globalData.get_config('config.home_site_logo_square'),
		        });
		    } else {
		        app.globalData.is_config(this, 'init_config');
		    }
		},
		
        init() {
            var user = app.globalData.get_user_info(this, 'init');

            if (user != false) {
				groups_base_info()
				.then(data=>{
					this.init_before(data)
					.then(()=>{
						this.get_other_data()
						.then(()=>{
							// 获取数据
							this.get_data_list();
						});
					});
				});
            } else {
                this.setData({
                    data_list_loding_status: 0,
                    data_bottom_line_status: false
                });
            }
        },
		
		// 设置本页参数
		init_before(data){
			var self = this;
			return new Promise((resolve, reject) => {
				self.setData({
					lang_info:data,
					nav_status_list:data.team_list_status_list_array,
				});
				uni.setNavigationBarTitle({
				    title: data.team_list_page_title
				});
				resolve();
			});
		},

        // 输入框事件
        input_event(e) {
            this.setData({
                input_keyword_value: e.detail.value
            });
        },

        // 获取数据
        get_data_list(is_mandatory) {
            // 分页是否还有数据
            if ((is_mandatory || 0) == 0) {
                if (this.data_bottom_line_status == true) {
                    return false;
                }
            } 
			
			// 加载loding
            uni.showLoading({
                title: this.lang_info.loading_tips
            });
            this.setData({
                data_list_loding_status: 1
            }); 
			
			// 参数
            var order_status = (this.nav_status_list[this.nav_status_index] || null) == null ? -1 : this.nav_status_list[this.nav_status_index]['value']; // 获取数据
			var self = this;
            uni.request({
                url: app.globalData.get_request_url('teamlist', 'index', 'groups'),
                method: 'POST',
                data: {
                    page: this.data_page,
                    keywords: this.input_keyword_value || '',
                    status: order_status,
                    is_more: 1,
                    team_flag: 1 //是获取拼团
                },
                dataType: 'json',
                success: (res) => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();console.log(res);

                    if (res.data.code == 0) {
                        if (res.data.data.data.length > 0) {
                            if (this.data_page <= 1) {
                                var temp_data_list = res.data.data.data; 
								
								// 下订单支付处理
                                if (this.load_status == 0) {
                                    if ((this.params.is_pay || 0) == 1 && (this.params.order_id || 0) != 0) {
                                        for (var i in temp_data_list) {
                                            if (this.params.order_id == temp_data_list[i]['id']) {
                                                this.pay_handle(this.params.order_id, i);
                                                break;
                                            }
                                        }
                                    }
                                }
                            } else {
                                var temp_data_list = this.data_list;
                                var temp_data = res.data.data.data;

                                for (var i in temp_data) {
                                    temp_data_list.push(temp_data[i]);
                                }
                            }

                            this.setData({
                                data_list: temp_data_list,
                                data_total: res.data.data.total,
                                data_page_total: res.data.data.page_total,
                                data_list_loding_status: 3,
                                data_page: this.data_page + 1,
                                load_status: 1,
                            }); 
							
							// 是否还有数据
                            if (this.data_page > 1 && this.data_page > this.data_page_total) {
                                this.setData({
                                    data_bottom_line_status: true
                                });
                            } else {
                                this.setData({
                                    data_bottom_line_status: false
                                });
                            }
                        } else {
                            this.setData({
                                data_list_loding_status: 0,
                                load_status: 1,
                                data_list: [],
                                data_bottom_line_status: false
                            });
                        }
                    } else {
						/*if(res.data.code == -400){
							clear_user_cach().then(()=>{
								self.get_data_list();
							});
							return false;
						}*/
						
                        this.setData({
                            data_list_loding_status: 0,
                            load_status: 1
                        });

                        if (app.globalData.is_login_check(res.data, this, 'get_data_list')) {
                            app.globalData.showToast(res.data.msg);
                        }
                    }
                },
                fail: () => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    this.setData({
                        data_list_loding_status: 2,
                        load_status: 1
                    });
                    app.globalData.showToast(this.lang_info.request_data_error_tips);
                }
            });
        },
		
		// 支付参数+红包数据单独调用
		get_other_data() {
			return new Promise((resolve, reject) => {
				uni.request({
					url: app.globalData.get_request_url('otherdata', 'index', 'groups'),
					method: 'POST',
					data: {
						is_more: 1,
						team_flag: 1 //是获取拼团
					},
					dataType: 'json',
					success: (res) => {
						console.log(res);
						if (res.data.code == 0) {
							var plugins_hongbao_data = res.data.data.plugins_hongbao_data || [];
							var is_show_hongbao = (plugins_hongbao_data.length > 0) ? true : false;
							this.setData({
								payment_list: res.data.data.payment_list || [],
								plugins_hongbao_data:plugins_hongbao_data,
								is_show_hongbao_popup:is_show_hongbao
							}); 
							resolve();
						} else {
							app.globalData.showToast(res.data.msg);
							reject(res.data.msg);
						}
					},
					fail: () => {
						uni.stopPullDownRefresh();
						app.globalData.showToast(this.lang_info.request_data_error_tips);
						reject(this.lang_info.request_data_error_tips);
					}
				});
			});
		},
	
		// 领取红包事件
		handle_hongbao_event(e) {
		    if(!app.globalData.is_single_page_check()) {
		        return false;
		    }
			var self = this;
		    var user = app.globalData.get_user_info(this, 'handle_hongbao_event');
		    if (user != false) {
				var order_id = e.currentTarget.dataset.value;
				uni.showLoading({
					title: this.lang_info.groups_hongbao_get_ing_text
				});
				uni.request({
					url: app.globalData.get_request_url('handlehongbao', 'index', 'groups'),
					method: 'POST',
					data: {
						"id": order_id
					},
					dataType: 'json',
					success: res => {
						uni.hideLoading();
						if (res.data.code == 0) {
							app.globalData.showToast(res.data.msg, "success");
							self.hongbao_popup_event_close();
							
							// 跳转到单独的提示页
							app.globalData.url_open("/pages/fcc/groups/hongbao/hongbao?id="+order_id);
							return false;
						} else {
							app.globalData.showToast(res.data.msg);
						}
					},
					fail: () => {
						uni.hideLoading();
						app.globalData.showToast(this.lang_info.request_data_error_tips);
					}
				});
		    }
		},

        // 滚动加载
        scroll_lower(e) {
            this.get_data_list();
        },
		
		// 红包弹窗关闭
		hongbao_popup_event_close(e) {
		    this.setData({
		        is_show_hongbao_popup: false
		    });
		},

        // 导航事件
        nav_event(e) {
            this.setData({
                nav_status_index: e.currentTarget.dataset.index || 0,
                data_page: 1
            });
            this.get_data_list(1);
        },

        // url事件
        url_event(e) {
            app.globalData.url_event(e);
        }
    }
};
</script>
<style>
@import './team.css';
</style>
